home *** CD-ROM | disk | FTP | other *** search
/ The PC-SIG Library 10 / The PC-Sig Library - Shareware for the IBM PC and Compatibles (PC-SIG)(Tenth Edition Disks 1-2804)(1991).iso / PC_SIGCD / 22 / 4 / DISK2247.ZIP / CBASE101.ZIP / ROLODECK.ZIP / FML.C < prev    next >
Text File  |  1990-06-21  |  5KB  |  181 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)fml.c    1.4 - 90/06/21" */
  5.  
  6. #include <blkio.h>    /* ansi compatibility macros */
  7.  
  8. /* ansi headers */
  9. #include <errno.h>
  10. /*#include <stddef.h>*/
  11. #include <stdio.h>
  12. /*#include <stdlib.h>*/
  13. /*#include <string.h>*/
  14.  
  15. /* macros */
  16. #define min(a,b)    ((a) < (b) ? (a) : (b))
  17.  
  18. /*man---------------------------------------------------------------------------
  19. NAME
  20.      fmltolfm - convert name from fml to lfm format
  21.  
  22. SYNOPSIS
  23.      int fmltolfm(t, s, n)
  24.      char *t;
  25.      const char *s;
  26.      size_t n;
  27.  
  28. DESCRIPTION
  29.      The fmltolfm function converts a name from the format "first
  30.      middle last" to "last first middle".  s points to the source
  31.      string in fml format.  t points to the target string to receive
  32.      the converted name.  t and s may point to the same string.  There
  33.      may be more than one middle name, or the middle name may be
  34.      absent.  All leading and trailing spaces must be removed from the
  35.      source string before calling fmltolfm.
  36.  
  37.      fmltolfm will fail if one or more of the following is true:
  38.  
  39.      [EINVAL]       t or s is the NULL pointer.
  40.  
  41. SEE ALSO
  42.      lfmtofml.
  43.  
  44. DIAGNOSTICS
  45.      Upon successful completion, a value of 0 is returned.  Otherwise,
  46.      a value of -1 is returned, and errno set to indicate the error.
  47.  
  48. ------------------------------------------------------------------------------*/
  49. int fmltolfm(t, s, n)
  50. char *t;
  51. const char *s;
  52. size_t n;
  53. {
  54.     char *p = NULL;
  55.     char *ts = NULL;    /* temporary string */
  56.  
  57.     /* validate arguments */
  58.     if (t == NULL || s == NULL) {
  59.         errno = EINVAL;
  60.         return -1;
  61.     }
  62.     if (n < 2) {
  63.         errno = 0;
  64.         return 0;
  65.     }
  66.  
  67.     /* find beginning of last name */
  68.     p = strrchr(s, ' ');
  69.     if (p == NULL) {
  70.         strncpy(t, s, n);
  71.         t[n - 1] = '\0';
  72.         errno = 0;
  73.         return 0;
  74.     }
  75.  
  76.     /* create temporary string */
  77.     ts = (char *)calloc((size_t)n, (size_t)1);
  78.     if (ts == NULL) {
  79.         errno = ENOMEM;
  80.         return -1;
  81.     }
  82.  
  83.     /* perform conversion */
  84.     strncpy(ts, p + 1, n);            /* copy last name */
  85.     ts[n - 1] = '\0';
  86.     strncat(ts, " ", n - strlen(ts));    /* add space after last name */
  87.     ts[n - 1] = '\0';
  88.     strncat(ts, s, n - strlen(ts));        /* copy beginning of name */
  89.     ts[min(n - 1, strlen(s))] = '\0';
  90.     strncpy(t, ts, n);            /* copy converted string to t */
  91.     t[n - 1] = '\0';
  92.  
  93.     /* free temporary string */
  94.     free(ts);
  95.  
  96.     errno = 0;
  97.     return 0;
  98. }
  99.  
  100. /*man---------------------------------------------------------------------------
  101. NAME
  102.      lfmtofml - convert name from lfm to fml format
  103.  
  104. SYNOPSIS
  105.      int lfmtofml(t, s, n)
  106.      char *t;
  107.      const char *s;
  108.      size_t n;
  109.  
  110. DESCRIPTION
  111.      The lfmtofml function converts a name from the format "last
  112.      first middle" to "first middle last".  s points to the source
  113.      string in lfm format.  t points to the target string to receive
  114.      the converted name.  t and s may point to the same string.  There
  115.      may be more than one middle name, or the middle name may be
  116.      absent.  All leading and trailing spaces must be removed from the
  117.      source string before calling lfmtofml.
  118.  
  119.      lfmtofml will fail if one or more of the following is true:
  120.  
  121.      [EINVAL]       t or s is the NULL pointer.
  122.  
  123. SEE ALSO
  124.      fmltolfm.
  125.  
  126. DIAGNOSTICS
  127.      Upon successful completion, a value of 0 is returned.  Otherwise,
  128.      a value of -1 is returned, and errno set to indicate the error.
  129.  
  130. ------------------------------------------------------------------------------*/
  131. int lfmtofml(t, s, n)
  132. char *t;
  133. const char *s;
  134. size_t n;
  135. {
  136.     char *p = NULL;
  137.     char *ts = NULL;    /* temporary string */
  138.  
  139.     /* validate arguments */
  140.     if (t == NULL || s == NULL) {
  141.         errno = EINVAL;
  142.         return -1;
  143.     }
  144.     if (n < 2) {
  145.         errno = 0;
  146.         return 0;
  147.     }
  148.  
  149.     /* find end of last name */
  150.     p = strchr(s, ' ');
  151.     if (p == NULL) {
  152.         strncpy(t, s, n);
  153.         t[n - 1] = '\0';
  154.         errno = 0;
  155.         return 0;
  156.     }
  157.  
  158.     /* create temporary string */
  159.     ts = (char *)calloc((size_t)n, (size_t)1);
  160.     if (ts == NULL) {
  161.         errno = ENOMEM;
  162.         return -1;
  163.     }
  164.  
  165.     /* perform conversion */
  166.     strncpy(ts, p + 1, n);            /* copy beginning of name */
  167.     ts[n - 1] = '\0';
  168.     strncat(ts, " ", n - strlen(ts));    /* add space before last name */
  169.     ts[n - 1] = '\0';
  170.     strncat(ts, s, n - strlen(ts));        /* copy last name */
  171.     ts[min(n - 1, strlen(s))] = '\0';
  172.     strncpy(t, ts, n);            /* copy converted string to t */
  173.     t[n - 1] = '\0';
  174.  
  175.     /* free temporary string */
  176.     free(ts);
  177.  
  178.     errno = 0;
  179.     return 0;
  180. }
  181.